﻿using System;
using System.Collections.Generic;
using System.Linq;
using QuanLyTiemThuocTay.cls;

namespace QuanLyTiemThuocTay.Report
{
    public partial class rptTonKho : DevExpress.XtraReports.UI.XtraReport
    {
        QLTTDataContext db = new QLTTDataContext();
        private int STT;
        int tongsoluong = 0;
        List<THUOC> thuocs = new List<THUOC>();
        public rptTonKho()
        {
            InitializeComponent();
            DisplayName = "Tồn kho";
            thuocs = db.THUOCs.ToList();
            STT = 0;
            try
            {
                DataSource = thuocs;
            }
            catch { }
        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            STT++;
            xrTT.Text = STT.ToString();
            try { xrTen.Text = "  "+thuocs[STT - 1].TenThuoc; }
            catch { }
            try { xrDVT.Text = "  " + db.DVTINHs.SingleOrDefault(x => x.MaDVTinh == thuocs[STT - 1].MaDVTinh).TenDVTinh; }
            catch { }
            try
            {
                int sl = soLuongConCuaThuoc(thuocs[STT - 1].MaThuoc);
                xrSoLuong.Text = sl + "";
                tongsoluong += sl;
            }
            catch { }
            xrTongSLuong.Text = tongsoluong + "";
        }

        private int soLuongConCuaThuoc(string mathuoc)
        {
            int soluong = 0;
            var tongnhap = db.CHITIETNHAPs.Where(x => x.MaThuoc == mathuoc).Sum(y => y.SoLuong);
            var tongxuat = db.CHITIETXUATs.Where(x => x.MaThuoc == mathuoc).Sum(y => y.SoLuong);
            if (tongxuat == null)
                tongxuat = 0;
            soluong = (int)(tongnhap - tongxuat);
            if (soluong < 0)
                soluong = 0;
            return soluong;
        }
    }
}
